using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Terminal.Advanced_AEUC_settings_controls.Settings_AUX;
using Auxiliaries;

namespace Terminal.Advanced_AEUC_settings_controls.Settings_Editing_Forms
{
    /// <summary>
    /// Form for displaying and editting special tab pages control properties
    /// </summary>
    public partial class AEUCTabPageProperties : Form
    {
        TabPage edtTab;
        GenTabPageProperties gtpProp;
        /// <summary>
        /// Public constructor
        /// </summary>
        /// <param name="iedtTab">Iniializes interface to this TabPage instance</param>
        public AEUCTabPageProperties(TabPage iedtTab)
        {
            InitializeComponent();
            edtTab = iedtTab;
            tbTabName.Text = edtTab.Text;
            tbTabId.Enabled = false;
            lblOMXParamStructId.Enabled = false;
        }
        /// <summary>
        /// Public constructor
        /// </summary>
        /// <param name="igtpProp">Iniializes interface to this GenTabPageProperties instance</param>
        public AEUCTabPageProperties(GenTabPageProperties igtpProp)
        {
            InitializeComponent();
            gtpProp = igtpProp;
            tbTabName.Text = gtpProp.TabName;
            tbTabId.Text = gtpProp.TabId.ToString();
            tbTabPortNum.Text = gtpProp.TabIdExtra.ToString();
            tbOMXParamStructMSGId.Text = gtpProp.TabRelMSGId.ToString();
            tbOMXParamStructMSGRqstId.Text = gtpProp.TabRelRqstMSGId.ToString();
            tbOMXParamStructMSGStatus.Text = gtpProp.TabRelMSGStatus.ToString();
            tbOMXParamStructCompId.Text = gtpProp.TabRelCompId.ToString();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            if (edtTab != null)
            {
                edtTab.Text = tbTabName.Text;
            }
            else if (gtpProp != null)
            {
                gtpProp.TabName = tbTabName.Text;
                uint id = 0;
                if (!uint.TryParse(tbTabId.Text, out id))
                {
                    UInt64 tmpVal;
                    if (!HEX_converter.ToBinary(tbTabId.Text, out tmpVal))
                        id = 0;
                    else
                        id = (uint)tmpVal;
                }
                gtpProp.TabId = id;
                uint size = 0;
                if (!uint.TryParse(tbTabPortNum.Text, out size))
                {
                    UInt64 tmpVal;
                    if (!HEX_converter.ToBinary(tbTabPortNum.Text, out tmpVal))
                        size = 0;
                    else
                        size = (uint)tmpVal;
                }
                gtpProp.TabIdExtra = size;

                uint compId = 0;
                if (!uint.TryParse(tbOMXParamStructCompId.Text, out compId))
                {
                    UInt64 tmpVal;
                    if (!HEX_converter.ToBinary(tbOMXParamStructCompId.Text, out tmpVal))
                        compId = 0;
                    else
                        compId = (uint)tmpVal;
                }
                gtpProp.TabRelCompId = compId;

                uint msgId = 0;
                if (!uint.TryParse(tbOMXParamStructMSGId.Text, out msgId))
                {
                    UInt64 tmpVal;
                    if (!HEX_converter.ToBinary(tbOMXParamStructMSGId.Text, out tmpVal))
                        msgId = 0;
                    else
                        msgId = (uint)tmpVal;
                }
                gtpProp.TabRelMSGId = msgId;

                uint msgRqstId = 0;
                if (!uint.TryParse(tbOMXParamStructMSGRqstId.Text, out msgRqstId))
                {
                    UInt64 tmpVal;
                    if (!HEX_converter.ToBinary(tbOMXParamStructMSGRqstId.Text, out tmpVal))
                        msgRqstId = 0;
                    else
                        msgRqstId = (uint)tmpVal;
                }
                gtpProp.TabRelRqstMSGId = msgRqstId;

                uint msgStat = 0;
                if (!uint.TryParse(tbOMXParamStructMSGStatus.Text, out msgStat))
                {
                    UInt64 tmpVal;
                    if (!HEX_converter.ToBinary(tbOMXParamStructMSGStatus.Text, out tmpVal))
                        msgStat = 0;
                    else
                        msgStat = (uint)tmpVal;
                }
                gtpProp.TabRelMSGStatus = msgStat;
            }
            Close();
        }
    }
}